package handling.channel.handler;

import client.MapleCharacter;
import client.MapleClient;
import client.Skill;
import client.SkillFactory;
import handling.channel.ChannelServer;
import handling.channel.PlayerStorage;
import handling.world.World.Alliance;
import handling.world.World.Guild;
import handling.world.guild.MapleGuild;
import handling.world.guild.MapleGuildResponse;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.mina.core.session.IoSession;
import server.MapleStatEffect;
import server.maps.MapleMap;
import tools.Pair;
import tools.data.LittleEndianAccessor;
import tools.packet.GuildPacket;

public class GuildHandler {

    private static final Map<String, Pair<Integer, Long>> invited = new HashMap();
    private static long nextPruneTime = System.currentTimeMillis() + 300000L;

    public static void DenyGuildRequest(String from, MapleClient c) {
        MapleCharacter cfrom = c.getChannelServer().getPlayerStorage().getCharacterByName(from);
        if ((cfrom != null) && (invited.remove(c.getPlayer().getName().toLowerCase()) != null)) {
            cfrom.getClient().getSession().write(GuildPacket.denyGuildInvitation(c.getPlayer().getName()));
        }
    }

    private static boolean isGuildNameAcceptable(String name) {
        return (name.getBytes().length >= 3) && (name.getBytes().length <= 12);
    }

    private static void respawnPlayer(MapleCharacter mc) {
        if (mc.getMap() == null) {
            return;
        }
        mc.getMap().broadcastMessage(GuildPacket.loadGuildName(mc));
        mc.getMap().broadcastMessage(GuildPacket.loadGuildIcon(mc));
    }

    public static void Guild(LittleEndianAccessor slea, MapleClient c) {
        long currentTime = System.currentTimeMillis();
        if (currentTime >= nextPruneTime) {
            Iterator itr = invited.entrySet().iterator();

            while (itr.hasNext()) {
                Map.Entry inv = (Map.Entry) itr.next();
                if (currentTime >= ((Long) ((Pair) inv.getValue()).right).longValue()) {
                    itr.remove();
                }
            }
            nextPruneTime += 300000L;
        }
        MapleCharacter chr = c.getPlayer();
        byte mode = slea.readByte();
        int guildId;
        String name;
        int cid;
        Skill skilli;
        int eff;
        switch (mode) {
            case 2:
                if ((chr.getGuildId() > 0) || (chr.getMapId() != 200000301)) {
                    chr.dropMessage(1, "不能创建家族\r\n已经有家族或没在家族中心");
                    return;
                }
                if (chr.getMeso() < 500000L) {
                    chr.dropMessage(1, "你没有足够的金币创建一个家族。");
                    return;
                }
                String guildName = slea.readMapleAsciiString();
                if (!isGuildNameAcceptable(guildName)) {
                    chr.dropMessage(1, "你不能使用这个名字。");
                    return;
                }
                guildId = Guild.createGuild(chr.getId(), guildName);
                if (guildId == 0) {
                    chr.dropMessage(1, "创建家族出错\r\n请重试一次.");
                    return;
                }
                chr.gainMeso(-500000L, true, true);
                chr.setGuildId(guildId);
                chr.setGuildRank((byte) 1);
                chr.saveGuildStatus();
                chr.finishAchievement(35);
                Guild.setGuildMemberOnline(chr.getMGC(), true, c.getChannel());
                c.getSession().write(GuildPacket.showGuildInfo(chr));
                Guild.gainGP(chr.getGuildId(), 500, chr.getId());
                chr.dropMessage(1, "恭喜你成功创建家族.");
                respawnPlayer(chr);
                break;
            case 5:
                if ((chr.getGuildId() <= 0) || (chr.getGuildRank() > 2)) {
                    return;
                }
                name = slea.readMapleAsciiString().toLowerCase();
                if (invited.containsKey(name)) {
                    /* 100 */ chr.dropMessage(5, "玩家 " + name + " 已经在邀请的列表，请稍后在试。");
                    /* 101 */ return;
                }
                /* 103 */ MapleGuildResponse mgr = MapleGuild.sendInvite(c, name);
                /* 104 */ if (mgr != null) /* 105 */ {
                    c.getSession().write(mgr.getPacket());
                } else {
                    /* 107 */ invited.put(name, new Pair(Integer.valueOf(chr.getGuildId()), Long.valueOf(currentTime + 1200000L)));
                }
                /* 109 */ break;
            case 6:
                /* 111 */ if (chr.getGuildId() > 0) {
                    /* 112 */ return;
                }
                /* 114 */ guildId = slea.readInt();
                /* 115 */ cid = slea.readInt();
                /* 116 */ if (cid != chr.getId()) {
                    /* 117 */ return;
                }
                /* 119 */ name = chr.getName().toLowerCase();
                /* 120 */ Pair gid = (Pair) invited.remove(name);
                /* 121 */ if ((gid != null) && (guildId == ((Integer) gid.left).intValue())) {
                    /* 124 */ chr.setGuildId(guildId);
                    /* 125 */ chr.setGuildRank((byte) 5);
                    /* 126 */ int s = Guild.addGuildMember(chr.getMGC());
                    /* 127 */ if (s == 0) {
                        /* 128 */ chr.dropMessage(1, "尝试加入的家族成员数已到达最高限制。");
                        /* 129 */ chr.setGuildId(0);
                        /* 130 */ return;
                    }
                    /* 132 */ c.getSession().write(GuildPacket.showGuildInfo(chr));
                    /* 133 */ MapleGuild gs = Guild.getGuild(guildId);
                    /* 134 */ for (byte[] pack : Alliance.getAllianceInfo(gs.getAllianceId(), true)) {
                        /* 135 */ if (pack != null) {
                            /* 136 */ c.getSession().write(pack);
                        }
                    }
                    /* 139 */ chr.saveGuildStatus();
                    /* 140 */ respawnPlayer(c.getPlayer());
                    /* 141 */                }
                break;
            case 7:
                /* 143 */ cid = slea.readInt();
                /* 144 */ name = slea.readMapleAsciiString();
                /* 145 */ if ((cid != chr.getId()) || (!name.equals(chr.getName())) || (chr.getGuildId() <= 0)) {
                    /* 146 */ return;
                }
                /* 148 */ Guild.leaveGuild(chr.getMGC());
                /* 149 */ c.getSession().write(GuildPacket.showGuildInfo(null));
                /* 150 */ break;
            case 8:
                /* 152 */ cid = slea.readInt();
                /* 153 */ name = slea.readMapleAsciiString();
                /* 154 */ if ((chr.getGuildRank() > 2) || (chr.getGuildId() <= 0)) {
                    /* 155 */ return;
                }
                /* 157 */ Guild.expelMember(chr.getMGC(), name, cid);
                /* 158 */ break;
            case 14:
                /* 160 */ if ((chr.getGuildId() <= 0) || (chr.getGuildRank() != 1)) {
                    /* 161 */ return;
                }
                /* 163 */ String[] ranks = new String[5];
                /* 164 */ for (int i = 0; i < 5; i++) {
                    /* 165 */ ranks[i] = slea.readMapleAsciiString();
                }
                /* 167 */ Guild.changeRankTitle(chr.getGuildId(), ranks);
                /* 168 */ break;
            case 15:
                /* 170 */ cid = slea.readInt();
                /* 171 */ byte newRank = slea.readByte();
                /* 172 */ if ((newRank <= 1) || (newRank > 5) || (chr.getGuildRank() > 2) || ((newRank <= 2) && (chr.getGuildRank() != 1)) || (chr.getGuildId() <= 0)) {
                    /* 173 */ return;
                }
                /* 175 */ Guild.changeRank(chr.getGuildId(), cid, newRank);
                /* 176 */ break;
            case 16:
                /* 178 */ if ((chr.getGuildId() <= 0) || (chr.getGuildRank() != 1) || (chr.getMapId() != 200000301)) {
                    /* 179 */ return;
                }
                /* 181 */ if (chr.getMeso() < 1500000L) {
                    /* 182 */ chr.dropMessage(1, "金币不足。");
                    /* 183 */ return;
                }
                /* 185 */ short bg = slea.readShort();
                /* 186 */ byte bgcolor = slea.readByte();
                /* 187 */ short logo = slea.readShort();
                /* 188 */ byte logocolor = slea.readByte();
                /* 189 */ Guild.setGuildEmblem(chr.getGuildId(), bg, bgcolor, logo, logocolor);
                /* 190 */ chr.gainMeso(-1500000L, true, true);
                /* 191 */ respawnPlayer(c.getPlayer());
                /* 192 */ break;
            case 17:
                /* 194 */ String notice = slea.readMapleAsciiString();
                /* 195 */ if ((notice.length() > 100) || (chr.getGuildId() <= 0) || (chr.getGuildRank() > 2)) {
                    /* 196 */ return;
                }
                /* 198 */ Guild.setGuildNotice(chr.getGuildId(), notice);
                /* 199 */ break;
            case 29:
                /* 201 */ skilli = SkillFactory.getSkill(slea.readInt());
                /* 202 */ if ((chr.getGuildId() <= 0) || (skilli == null) || (skilli.getId() < 91000000)) {
                    /* 203 */ return;
                }
                /* 205 */ eff = Guild.getSkillLevel(chr.getGuildId(), skilli.getId()) + 1;
                /* 206 */ if (eff > skilli.getMaxLevel()) {
                    /* 207 */ return;
                }
                /* 209 */ MapleStatEffect skillid = skilli.getEffect(eff);
                /* 210 */ if ((skillid.getReqGuildLevel() <= 0) || (chr.getMeso() < skillid.getPrice())) {
                    /* 211 */ return;
                }
                /* 213 */ if (Guild.purchaseSkill(chr.getGuildId(), skillid.getSourceId(), chr.getName(), chr.getId())) {
                    /* 216 */ chr.gainMeso(-skillid.getPrice(), true);
                    /* 217 */                }
                break;
            case 30:
                /* 219 */ skilli = SkillFactory.getSkill(slea.readInt());
                /* 220 */ if ((c.getPlayer().getGuildId() <= 0) || (skilli == null)) {
                    /* 221 */ return;
                }
                /* 223 */ eff = Guild.getSkillLevel(chr.getGuildId(), skilli.getId());
                /* 224 */ if (eff <= 0) {
                    /* 225 */ return;
                }
                /* 227 */ MapleStatEffect skillii = skilli.getEffect(eff);
                /* 228 */ if ((skillii.getReqGuildLevel() < 0) || (chr.getMeso() < skillii.getExtendPrice())) {
                    /* 229 */ return;
                }
                /* 231 */ if (Guild.activateSkill(chr.getGuildId(), skillii.getSourceId(), chr.getName())) {
                    /* 234 */ chr.gainMeso(-skillii.getExtendPrice(), true);
                    /* 235 */                }
                break;
            case 31:
                /* 237 */ cid = slea.readInt();
                /* 238 */ if ((chr.getGuildId() <= 0) || (chr.getGuildRank() > 1)) {
                    /* 239 */ return;
                }
                /* 241 */ Guild.setGuildLeader(chr.getGuildId(), cid);
                /* 242 */ break;
            case 3:
            case 4:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
            case 26:
            case 27:
            case 28:
            default:
                /* 262 */ System.out.println("未知家族操作类型: " + mode + " " + slea.toString());
        }
    }
}

/* Location:           C:\Users\文炳\Desktop\CXFBY\114Maplewing.jar
 * Qualified Name:     handling.channel.handler.GuildHandler
 * JD-Core Version:    0.6.2
 */